/**
 * futures : masterlink.futures.controller.IndexController.java
 */
package masterlink.futures.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import masterlink.futures.dao.FunctionDao;
import masterlink.futures.util.WebUtils;
import masterlink.futures.vo.Employee;
import masterlink.futures.vo.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import baytony.util.Util;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/9/30
 * @version 1.0.0	
 */
@Controller
@RequestMapping(value = "/header")
public class HeaderController extends BasicController{
	
	@Autowired
	@Qualifier("FunctionDao")
	private FunctionDao functionDao;
	
	@RequestMapping(value = "/index")
	public String header(HttpServletRequest req){
		Employee emp=WebUtils.getLogEmployee(req);
		log.trace("Employee {} loading header",emp);
		if(Util.isEmptyList(emp.getAllFun())){
			log.trace("Function is null, reloading functions for {}",emp);
			emp.setAllFun(functionDao.findByEmp(emp));
			WebUtils.setFunction(getIndexFunction(emp.getAllFun()), req);
		}
		return "header/header";
	}
	
	@RequestMapping(value = "/reload")
	public String reload(HttpServletRequest req){ 
		Employee emp=WebUtils.getLogEmployee(req);
		log.trace("Clear functions for {}",emp);
		emp.setAllFun(null);
		return header(req);
	}
	
	private Function getIndexFunction(List<Function> all){
		if(!Util.isEmptyList(all)){
			for(Function f:all){
				if(f.isIndex()){
					return f;
				}
			}
			return all.get(0);
		}
		return null;
	}
	
}
